﻿
@{
    Layout = null;
}


<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">中文全称</label>
        <div class="layui-input-inline ">
            <input type="text" id="name" name="name" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入中文全称">
        </div>
        <label class="layui-form-label">供应商编码</label>
        <div class="layui-input-inline">
            <input type="text" id="supplierCode" name="supplierCode" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入供应商编码">
        </div>
        <div class="layui-btn-container">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="btnSeash">查询</button>
            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm">重置</button>
            <button type="button" class="layui-btn layui-btn-sm" onclick="insert()">
                <i class="layui-icon layui-icon-add-1">录入</i>
            </button>
            <button type="button" class="layui-btn layui-btn-sm layui-btn-warm" lay-event="delete">
                <i class="layui-icon layui-icon-delete">批量删除</i>
            </button>
        </div>
    </div>
    <script type="text/html" id="btn">
        <button type="button" class="layui-btn layui-btn-xs" lay-event="update">
            <i class="layui-icon layui-icon-edit">编辑</i>
        </button>
        <button type="button" class="layui-btn layui-btn-xs" lay-event="delete">
            <i class="layui-icon layui-icon-delete">删除</i>
        </button>
    </script>
</form>
    <table class="layui-hide" id="GetsupplierList" name="GetsupplierList"></table>

    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
    <script>
        layui.use('table', function () {
            var table = layui.table;
            var from = layui.from;
            var laydata = layui.laydata;
            // 已知数据渲染
            var inst = table.render({
                elem: '#GetsupplierList',
                url: '/Supplier/GetSupplierList',
                cols: [[ //标题栏
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'name', title: '中文全称 ', width: 120 },
                    { field: 'supplierCode', title: '供应商编码 ', width: 120 },
                    { field: 'enterpriseAttribute', title: '企业属性', width: 120 },
                    { field: 'address', title: '地址 ', width: 120 },
                    { field: 'mainContact', title: '主联系人 ', width: 120 },
                    { field: 'telephone', title: '电话', width: 120 },
                    { title: '操作 ', width: 160, toolbar:'#btn' }
                ]],
                //skin: 'line', // 表格风格
                //even: true,
                page: true, // 是否显示分页
                height: 500,
                limits: [5, 10, 20,30],
                limit: 10,// 每页默认显示的数量
                where: {
                    pageIndex: 1,
                    pageSize: 10
                }
            });

            //查询
            $('#btnSeash').on('click', function () {
                table.reload('GetsupplierList', {
                    page: { curr: 1 },
                    where: {
                        name: $('#name').val(),
                        supplierCode: $('#supplierCode').val(),
                    }
                })
                return false;
            })

            //删除
            // 触发单元格工具事件
            table.on('tool(GetsupplierList)', function (obj) { // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                // console.log(obj)
                if (obj.event === 'delete') {
                    layer.confirm('确定要删除吗', function (index) {
                        var array = [];
                        array.push(data.id);
                        deleteSupplier(array);
                        layer.close(index)
                    })
                }
                if (obj.event =='update') {
                    layer.open({
                        type: 2, // page 层类型
                        area: ['500px', '500px'],
                        title: '供应商编辑',
                        shade: 0.6, // 遮罩透明度
                        shadeClose: true, // 点击遮罩区域，关闭弹层
                        maxmin: true, // 允许全屏最小化
                        anim: 0, // 0-6 的动画形式，-1 不开启
                        content: '/Supplier/Update?id='+ data.id,
                        end: function () {
                            location.reload()
                        }
                    });
                }
            });
            // 批量删除按钮点击事件
            $('.layui-btn-warm').on('click', function () {
                var checkStatus = table.checkStatus('GetsupplierList');
                var data = checkStatus.data;
                if (data.length === 0) {
                    layer.msg('请选择要删除的数据');
                    return;
                }
                layer.confirm('确定删除选中的 ' + data.length + ' 条数据吗？', function (index) {
                    var ids = data.map(item => item.id);
                    deleteSupplier(ids);
                    layer.close(index);
                });
            });
        });

        var deleteSupplier = function (ids) {
            $.ajax({
                type: 'get',
                url: '/Supplier/DeleteSupplier',
                dataType: 'json',
                data: {
                    ids:ids.toString()
                },
                success: function (res) {
                    if (res > 0) {
                        layer.msg('删除成功')
                        return false;
                        location.href = '/Supplier/GetSupplierList'
                    }
                }
            })
        }


        // 在此处输入 layer 的任意代码
        function insert() {
            layer.open({
                type: 2, // page 层类型
                area: ['500px', '500px'],
                title: '供应商录入',
                shade: 0.6, // 遮罩透明度
                shadeClose: true, // 点击遮罩区域，关闭弹层
                maxmin: true, // 允许全屏最小化
                anim: 0, // 0-6 的动画形式，-1 不开启
                content: '/Supplier/Add'
            });
        }
    </script>

